package com.hqq.exercise.string;

/**
 * StringOrder 字符串排列
 * 题目描述:
 * 输入一个字符串，打印出该字符串中字符的所有排列
 * 样例输入:
 * abc
 * 样例输出:
 * abc,acb,bac,bca,cab,cba
 * Created by heqianqian on 2017/8/15.
 */
public class StringOrder {

    public static void orderString(String str) {
        int count = 0;
        if (str == null || str.trim().equals("")) {
            System.out.println("字符串为空");
            return;
        }
        char[] chs = str.toCharArray();//字符数组
        int point = 0;
        System.out.print(chs);
        System.out.print(" ");
        count++;//打印个数
        //交换第一个节点和之后的节点
        char temp1 = chs[point];
        chs[point] = chs[++point];
        chs[point] = temp1;
        //交换第一个和之后的节点
        while (!String.valueOf(chs).equals(str)) {
            System.out.print(chs);
            System.out.print(" ");
            count++;
            if (point == chs.length - 1) {
                char temp = chs[point];
                chs[point] = chs[0];
                chs[0] = temp;
                point = 0;
            } else {
                char temp = chs[point];
                chs[point] = chs[++point];
                chs[point] = temp;
            }
        }
        System.out.println("\n总共" + count + "个数");
    }
}